<template>
    <div class="update-birthday">
        <van-datetime-picker
           v-model="currentDate"
           type="date"
           title="选择出生年月日"
           :min-date="minDate"
           :max-date="maxDate"
           @confirm="onConfirm"
           @cancel ="$emit('close')"
        />
    </div>
  </template>

<script>
import { updateUserProfile } from '@/api/user'
import dayjs from 'dayjs'
export default {
  name: 'UpdateName',
  components: {},
  props: {
    value: {
      type: String,
      required: true

    }
  },
  data () {
    return {
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)

    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onConfirm (value) {
      try {
        const select = dayjs(value).format('YYYY-MM-DD')
        if (select === this.value) {
          this.$toast('日期未修改')
          return
        }
        this.$toast.loading({
          duration: 0, // 持续时间，0表示持续展示不停止
          forbidClick: true, // 是否禁止背景点击
          message: '生日修改中...' // 提示消息
        })
        await updateUserProfile({
          birthday: select
        })
        this.$emit('input', select)
        this.$emit('close')
        this.$toast.success('生日更新成功')
      } catch (err) {
        window.console.log(err)
        this.$toast.fail('生日更改失败')
      }
    }
  }
}
</script>

  <style scoped lang="less">
 .field-wrap {
  padding: 20px;
  background-color: #d8d8d8;
}
  </style>
